package com.gzw.shop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.gzw.shop.App;
import com.gzw.shop.Contans;
import com.gzw.shop.R;
import com.gzw.shop.activity.AddressListActivity;
import com.gzw.shop.activity.LoginActivity;
import com.gzw.shop.activity.MyFavoritesActivity;
import com.gzw.shop.activity.MyOrderActivity;
import com.gzw.shop.bean.User;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class MineFragment extends BaseFragment {

    @BindView(R.id.img_head)
    CircleImageView mImgHead;
    @BindView(R.id.txt_username)
    TextView mTxtUsername;
    @BindView(R.id.txt_my_orders)
    TextView mTxtMyOrders;
    @BindView(R.id.txt_my_favorite)
    TextView mTxtMyFavorite;
    @BindView(R.id.txt_my_address)
    TextView mTxtMyAddress;
    @BindView(R.id.btn_logout)
    Button mBtnLogout;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initView() {
        init();
    }

    @Override
    public void initData() {

    }

    private void init() {
        User user = App.getInstance().getUser();
        showUser(user);
    }

    @OnClick(R.id.txt_username)
    public void goToLogin() {

        if (App.getInstance().getUser() == null) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivityForResult(intent, Contans.REQUEST_CODE);
        }
    }

    @OnClick(R.id.btn_logout)
    public void logOut() {
        App.getInstance().clearUser();
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivityForResult(intent, Contans.REQUEST_CODE);
    }

    @OnClick(R.id.txt_my_address)
    public void gotoAddress() {
        Intent intent = new Intent(getActivity(), AddressListActivity.class);
        startActivityForResult(intent, Contans.REQUEST_CODE, true);
    }

    @OnClick(R.id.txt_my_favorite)
    public void gotoFavorite() {
        Intent intent = new Intent(getActivity(), MyFavoritesActivity.class);
        startActivity(intent, true);
    }

    @OnClick(R.id.txt_my_orders)
    public void gotoOrders() {
        Intent intent = new Intent(getActivity(), MyOrderActivity.class);
        startActivity(intent, true);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Contans.REQUEST_CODE && resultCode == getActivity().RESULT_OK) {
            User user = App.getInstance().getUser();
            showUser(user);
        }
    }

    private void showUser(User user) {

        if (user != null) {
            mTxtUsername.setText(user.getUsername());
            Picasso.with(getActivity()).load(user.getLogo_url()).into(mImgHead);
            mBtnLogout.setVisibility(View.VISIBLE);
        } else {
            mTxtUsername.setText("点击登录");
            mBtnLogout.setVisibility(View.GONE);
            mImgHead.setImageResource(R.drawable.default_head);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        User user = App.getInstance().getUser();
        showUser(user);
    }
}
